﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SistemaBastanteo.Negocio;
using SistemaBastanteo.Dominio;

namespace SistemaBastanteo.Controllers
{
    public class PoderController : Controller
    {

        //Dependencias
        public IMantenimientoService AdminService { get; set; }
        //

        // GET: /Poder/

        public ActionResult Index()
        {
            ICollection<Poder> modelo = AdminService.ListarPoder();
            
            return View(modelo);
        }

        //
        // GET: /Poder/Details/5

        public ActionResult Details(int id)
        {
            Poder modelo = AdminService.ObtenerPoder(id);

            return View(modelo);
        }

        //
        // GET: /Poder/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Poder/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // TODO: Add insert logic here
                    string codigoCorto  = (string)collection["CodigoCorto"];
                    string descripcion  = (string)collection["Descripcion"];
                    string tipoProducto = (string) collection["TipoProducto"];

                    if (AdminService.ObtenerPoderPorCodigoCorto(codigoCorto).Count > 0)
                        throw new Exception("El Poder ingresado ya existe");

                    AdminService.RegistrarPoder(codigoCorto, descripcion, tipoProducto);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }

            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex.Message);
                return View();
            }
        }
        
        //
        // GET: /Poder/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Poder/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Poder/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Poder/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
